//
//  RAPEmoticonModel.swift
//  COPYCAT-Weibo
//
//  Created by 闫萌 on 1/7/16.
//  Copyright © 2016 RendezvousAuParadis. All rights reserved.
//

import UIKit

class RAPEmoticonModel: NSObject, NSCoding {
    /// 图片表情的中文释义
    var chs: String?
    /// 图片表情所对应的图片名
    var png: String? 
    /// emoji所代表的字符串
    var code: String? { // 在赋值时就进行转换
        didSet {
            guard let opCode = code else {
                code = nil
                return
            }
            code = NSString.emojiWithStringCode(opCode)
        }
    }
    /// 表情类型：1是emoji，0是图片表情
    var type: String?
    
    init(dict: [String : AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        // 留空
    }
    
    ///  比较的方法
    ///
    ///  - parameter object: 要比较的对象
    ///
    ///  - returns: bool
    override func isEqual(object: AnyObject?) -> Bool {
        let model = object as! RAPEmoticonModel
        if model.type == "0" { // 表情
            if (chs! as NSString).isEqualToString(model.chs!) {
                
                return true
            }
        } else { // emoji
            if (code! as NSString).isEqualToString(model.code!) {
                
                return true
            }
        }
        return false
    }
    
    // MARK: - NSCoding
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(chs, forKey: "chs")
        aCoder.encodeObject(png, forKey: "png")
        aCoder.encodeObject(code, forKey: "code")
        aCoder.encodeObject(type, forKey: "type")
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init()
        self.chs = aDecoder.decodeObjectForKey("chs") as? String
        self.png = aDecoder.decodeObjectForKey("png") as? String
        self.code = aDecoder.decodeObjectForKey("code") as? String
        self.type = aDecoder.decodeObjectForKey("type") as? String
    }
}
